<?php
namespace app\service;

use app\exception\ErrnoPlus;
use app\model\AdminAccountModel;
use share\service\cache\CommonCacheKeyService;
use share\exception\ApiException;
use share\service\BaseModelService;
use share\service\RedisService;
use share\utils\Helper;

class AdminAcountService extends BaseModelService{

    public static function getModel()
    {
        return new AdminAccountModel;
    }

    public static function getByAccount($account,$filter = []){
        $filter['account'] = $account;
        return self::getByFilter($filter);
    }

    

    public static function login($username,$password){
        $admin = self::getByAccount($username);
        if(empty($admin)){
            throw new ApiException("用户不存在",ErrnoPlus::USER_NOT_EXISTS);
        }

        // TODO 暂时直接比较
        if($admin['password'] !== $password){
            throw new ApiException("账号密码错误",ErrnoPlus::PASSWORD_ERROR);
        }
        unset($admin['password']);

        $token = self::auth($admin);
        $admin['token'] = $token;
        return $admin;
    }


    public static function auth($userInfo){
        $token = Helper::getUnique();
        $key = self::getCacheKey($token);
        RedisService::instance(RedisService::CACHE_DB_BASE)->setJson($key,$userInfo,1800);
        return $token;
    }

    public static function getLoginAdminInfo($token){
        $key = self::getCacheKey($token);
        return RedisService::instance(RedisService::CACHE_DB_BASE)->getJson($key);
    }

    public static function keepLoginByToken($token){
        $key = self::getCacheKey($token);
        return RedisService::instance(RedisService::CACHE_DB_BASE)->expire($key,1800);
    }

    public static function getCacheKey($token){
        return CommonCacheKeyService::getCacheKey(CommonCacheKeyService::CACHE_KEY_LOGIN_ACCOUNT_MANAGER ,['token' => $token]);

    }


    public static function buildCondition($filter)
    {
        return [];
    }
   

    /**
     * 添加管理员
     */
    public static function addAdmin($admin){
        $account =self::getByAccount($admin['account']);
        if($account){
            throw new ApiException("account is exist",ErrnoPlus::USER_EXISTS);
        }
        // TODO 密码安全性
        return self::add($admin);
    }

    /**
     * 修改管理员信息
     */
    public static function updateAdmin($admin){
        $id =  $admin['id'];
        $origin = self::getBase($id);
        if(!$origin){
            throw new ApiException("update account is not exist",ErrnoPlus::USER_NOT_EXISTS);
        }
        if(isset($admin['account']) && $admin['account'] && $origin['account'] !== $admin['account']  ){
            $newAccount = self::getByAccount($admin['account']);
            if($newAccount){
                throw new ApiException("update account is already exist",ErrnoPlus::USER_EXISTS);
            }
        }
        unset($admin['id']);
        // TODO 密码安全性
        return self::update($id,$admin);
    }

    /**
     * 删除管理员
     */
    public static function delAdmin($id){
        AdminPathinfoService::delAllAuthListByUser($id);
        self::del($id);
    }

    public static function updatePassword($id,$newPass,$oldPass){
        $origin = self::getBase($id);
        if(!$origin){
            throw new ApiException("update account is not exist",ErrnoPlus::USER_NOT_EXISTS);
        }

        // TODO 密码安全性
        if($origin['password'] !== $oldPass){
            throw new ApiException("old password is error",ErrnoPlus::OLD_PASSWORD_ERROR);
        }
        return self::setAdminPassword($id,$newPass);
    }

    /**
     * 设置管理员密码
     */
    public static function setAdminPassword($id,$newPass){
        // TODO 密码安全性
        return self::update($id,[
            'password' => $newPass,
        ]);
    }
}